package com.mbt.mobile.bluetooth.screen;

import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.mbt.mobile.bluetooth.manager.BluetoothConnectionManager;

/**
 * The Class RemoteDesktopScreen.
 */
public class RemoteDesktopScreen extends Canvas implements CommandListener{
	
		/** The img. */
		private Image img;
		
		/** The pointer img. */
		private Image pointerImg;
		
		/** The trans x. */
		private int transX = 0;
		
		/** The trans y. */
		private int transY = 0;
		
		/** The key code. */
		private int keyCode = 0;

		/** The pointer x. */
		private int pointerX = 0;
		
		/** The pointer y. */
		private int pointerY = 0;

		/** The img width. */
		int imgWidth = 0;
		
		/** The img height. */
		int imgHeight = 0;

		/** The can width. */
		int canWidth = 0;
		
		/** The can height. */
		int canHeight = 0;
		
		private Command refresh;
		private Command startMenu;
		private Command tab;
		private Command keyDown;
		private Command keyUp;
		private Command keyleft;
		private Command keyRight;
		private Command keyEnter;
		private Command close;
		
		/**
		 * Instantiates a new test canvas.
		 *
		 * @param img the img
		 */
		private BluetoothConnectionManager bluetoothConnectionManager;

		
		
		public RemoteDesktopScreen(Image img, BluetoothConnectionManager bluetoothConnectionManager) {
			refresh = new Command("Refresh", Command.OK, 0);
			startMenu = new Command("Start Menu", Command.CANCEL, 7);
			keyUp = new Command("Key UP", Command.CANCEL, 5);
			keyDown = new Command("Key Down", Command.CANCEL, 1);
			keyleft = new Command("Key Left", Command.CANCEL, 2);
			keyRight = new Command("Key Right", Command.CANCEL, 3);
			tab = new Command("Tab", Command.CANCEL, 4);
			keyEnter = new Command("Enter", Command.CANCEL, 0);
			close = new Command("Close", Command.CANCEL, 6);
			addCommand(refresh);
			
			addCommand(startMenu);
			addCommand(keyDown);
			addCommand(keyleft);
			addCommand(keyRight);
			addCommand(keyUp);
			addCommand(keyEnter);
			addCommand(close);
			addCommand(tab);
			
			setCommandListener(this);
			
			this.img = img;
			this.bluetoothConnectionManager= bluetoothConnectionManager;
			try {
				this.pointerImg = Image.createImage("/pointer.png");
			} catch (IOException e) {
				e.printStackTrace();
			}

			transX = 0;
			transY = 0;
			imgWidth = img.getWidth();
			imgHeight = img.getHeight();

			canWidth = getWidth();
			canHeight = getHeight();

		}

		/**
		 * Reload image.
		 * @param img2 the img2
		 */
		public void reloadImage(Image image) {
			System.gc();
			if(image!= null){
				this.img = Image.createImage(image);
				repaint();
			}
		}

		/**
		 * Increase xy.
		 * @param x the x
		 * @param y the y
		 */
		public void increaseXY(int x, int y) {

			pointerX += x;
			pointerY += y;

			// right move
			if (pointerX > -transX + canWidth - 15) {
				pointerX -= x;
				transX -= x;
			}
			if (pointerY > -transY + canHeight - 15) {
				pointerY -= y;
				transY -= y;
			}

			// left move
			if ((transX + pointerX) == 0 && pointerX > 0) {
				transX -= x;
			}
			if (transY + pointerY == 0 && pointerY > 0) {
				transY -= y;
			}

			// pointer reset
			if (pointerX < 0) {
				pointerX = 0;
			}
			if (pointerY < 0) {
				pointerY = 0;
			}

			// no move
			if (transX > 0 || transY > 0) {
				transX += x;
				transY += y;
			}
			if (((imgWidth + transX) < canWidth)
					|| ((imgHeight + transY) < canHeight)) {
				transX += x;
				transY += y;
			}

		}

		/* (non-Javadoc)
		 * @see javax.microedition.lcdui.Canvas#paint(javax.microedition.lcdui.Graphics)
		 */
		public void paint(Graphics g) {
			int width = this.getWidth();
			int height = this.getHeight();

			g.setGrayScale(255);
			g.fillRect(0, 0, width - 1, height - 1);
			g.setGrayScale(0);
			g.drawRect(0, 0, width - 1, height - 1);

			g.translate(transX, transY);
			 bluetoothConnectionManager.sendMessages("IMGH:"+img.getHeight());
			g.drawImage(img, 0, 0, Graphics.TOP | Graphics.LEFT);

			g.drawImage(pointerImg, pointerX, pointerY, Graphics.TOP | Graphics.LEFT);

			  bluetoothConnectionManager.sendMessages("MOUSE##" + pointerX + "##"
					+ pointerY);

		}

		/* (non-Javadoc)
		 * @see javax.microedition.lcdui.Canvas#keyPressed(int)
		 */
		protected void keyPressed(int keyCode) {
			int gameAction = getGameAction(keyCode);
			System.out.println();
			this.keyCode = keyCode;
			if (keyCode == 42) {
				bluetoothConnectionManager.sendMessages("MOUSECLICK##"+ pointerX + "##" + pointerY);
				//sendImageReloadRequest();
			}else if (keyCode == 35) {
				bluetoothConnectionManager.sendMessages("RIGHTCLICK##"+ pointerX + "##" + pointerY);
				sendImageReloadRequest();
				return;
			}else if (keyCode >= 48 && keyCode <= 57 ) {
				
				bluetoothConnectionManager.sendMessages("KEYPRESS##"+ keyCode);
				//sendImageReloadRequest();
				return;
			}
			switch (gameAction) {
			case UP:
				increaseXY(0, -15);
				break;
			case DOWN:
				increaseXY(0, 15);
				break;
			case LEFT:
				increaseXY(-15, 0);
				break;
			case RIGHT:
				increaseXY(15, 0);
				break;
			}
			repaint();
		}
		
		private void sendImageReloadRequest(){
			try {
				System.gc();
				Thread.sleep(200);
				bluetoothConnectionManager.sendMessages("RELOADIMG");
				Thread.sleep(300);
				reloadImage(bluetoothConnectionManager.getImage());
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}

		public void commandAction(Command command, Displayable displayable) {
			if (command == refresh) {
				sendImageReloadRequest();
				repaint();
			}else if (command == startMenu) {
				bluetoothConnectionManager.sendMessages("STARTMENU");
			}else if (command == keyUp) {
				bluetoothConnectionManager.sendMessages("KEYUP");
			}else if (command == keyDown) {
				bluetoothConnectionManager.sendMessages("KEYDOWN");
			}else if (command == keyleft) {
				bluetoothConnectionManager.sendMessages("KEYLEFT");
			}else if (command == keyRight) {
				bluetoothConnectionManager.sendMessages("KEYRIGHT");
			}else if (command == keyEnter) {
				bluetoothConnectionManager.sendMessages("ENTER");
			}else if (command == tab) {
				bluetoothConnectionManager.sendMessages("TAB");
			}else if (command == close) {
				bluetoothConnectionManager.sendMessages("CLOSE");
			}
		}
		
	}
